package com.dao;

import com.bean.Class;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassDao {
    //获取学生信息列表
    public static List<Class> getList() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Class> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = BaseDao.getConnection();
            stmt = conn.prepareStatement("SELECT * FROM class");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Class cla = new Class();
                cla.setClass_id(rs.getString(1));
                cla.setClass_name(rs.getString(2));
                cla.setAcademy(rs.getStirng(3));

                list.add(cla);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stmt, conn);
        }
        return list;
    }

    //增加学生信息


    //根据学号查找学生信息
    public static Class searchClass(String id){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rst = null;
        Class cla = new Class();
        try{
            conn = BaseDao.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM class WHERE class_id=?");
            pstmt.setString(1,id);
            rst = pstmt.executeQuery();
            if(rst.next()){
                cla.setClass_id(rst.getString("class_id"));
                cla.setClass_naem(rst.getString("class_name"));
                cla.setAcademy(rst.getString("academy"));

                return cla;
            }else{
                return  null;
            }
        }catch(SQLException se){
            return null;
        }finally{
            try{
                conn.close();
            }catch(SQLException se){
            }
        }
    }



    //获取单个学生对象

    //修改功能

    public static void updateClass(Class class) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = BaseDao.getConnection();
            stmt = conn.prepareStatement("UPDATE class SET class_id=?,class_name=?, academy where class_id=?");
            stmt.setString(1, class.getClass_id());
            stmt.setString(2, class.getClass_name());
            stmt.setString(3, class.getAcademy());

            
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeAll(rs, stmt, conn);
        }
    }




    //查询功能，根据模糊查询学号返回所有学生信息

}